define(function(require, exports){
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var $ = require('jquery');
	var util = require('util');
	var dialog = require('@base/dialog').base;

	var AdImport = view.widget.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'listUrl': 'channelInfo/listByIds',
				'layout': {
					"module": dialog,
					"title": LANG('导入渠道'),
					"silence": false,
					"buttonConfig": {
						"ok": {
							"value": LANG('确定')
						}
					},
					'size': 'm'
				},
				'view_model': {
					// 媒体广告位ID
					channelIds: ''
				}
			});
			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();
			el.append($([
				'<div class="M-formConGroup">',
					'<label class="group-label">' + LANG('请输入渠道ID：') + '</label>',
					'<div class="group-content">',
						'<textarea ms-duplex="channelIds" rows="10" cols="70" placeholder="'+ LANG('请输入渠道ID，多个ID请用回车键换行隔开') +'"/>',
					'</div>',
				'</div>'
			].join('')));
			self.buildFromTemplate({
			});
		}
		,getValue: function(){
			var vm = this.vm.$;
			var cs = this.$;
			var channelIds = [];
			var value = vm.channelIds.split('\n');
			value.forEach(function(item){
				var trimItem = util.trim(item);
				if (trimItem) {
					channelIds.push(trimItem);
				}
			});
			var result = {
				// 'ids': channelIds.join(',')
				'ids': channelIds
			};
			return result;
		}
		,setValue: function(data){
			var vm = this.vm.$;
			var cs = this.$;
			vm.channelIds = data.ids.join('\n') || '';
			return this;
		}
		,reset: function(){
			var cs = this.$;
			this.vm.reset();
			return this;
		}
		,validate: function(data){
			if (!data.ids.length) {
				pubjs.alert(LANG('请输入媒体ID！'));
				return false;
			}
			return true;
		}
		,onDialogOk: function(){
			var self = this;
			var data = self.getValue();
			var listUrl = this.getConfig('listUrl');
			if (self.validate(data)) {
				pubjs.loading.show();
				//获取输入ID对应的数据
				pubjs.mc.send(listUrl, data, self.afterSave.bind(self));
			}
			return false;
		}
		,afterSave: function(err, data){
			pubjs.loading.hide();
			if (err) {
				pubjs.alert(err.message);
				return false;
			}
			if(data && data.total == 0) {
				return pubjs.alert(LANG("导入的渠道失败，请检查相应渠道ID是否正确"))
			}
			
			this.fire('adImportOk', data);
			this.hide();
		}
	});
	exports.adImport = AdImport;
	
	var Spotmport = view.widget.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'listUrl': 'spot/listByIds',
				'layout': {
					"module": dialog,
					"title": LANG('导入广告位'),
					"silence": false,
					"buttonConfig": {
						"ok": {
							"value": LANG('确定')
						}
					},
					'size': 'm'
				},
				'view_model': {
					// 广告位ID
					spotIds: ''
				}
			});
			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();
			el.append($([
				'<div class="M-formConGroup">',
				'<label class="group-label">' + LANG('请输入广告位ID：') + '</label>',
				'<div class="group-content">',
				'<textarea ms-duplex="spotIds" rows="10" cols="70" placeholder="'+ LANG('请输入广告位ID，多个ID请用回车键换行隔开') +'"/>',
				'</div>',
				'</div>'
			].join('')));
			self.buildFromTemplate({
			});
		}
		,getValue: function(){
			var vm = this.vm.$;
			var cs = this.$;
			var spotIds = [];
			var value = vm.spotIds.split('\n');
			value.forEach(function(item){
				var trimItem = util.trim(item);
				if (trimItem) {
					spotIds.push(trimItem);
				}
			});
			var result = {
				'ids': spotIds.join(',')
			};
			return result;
		}
		,setValue: function(data){
			var vm = this.vm.$;
			var cs = this.$;
			vm.spotIds = data.ids.join('\n') || '';
			return this;
		}
		,reset: function(){
			var cs = this.$;
			this.vm.reset();
			return this;
		}
		,validate: function(data){
			if (!data.ids.length) {
				pubjs.alert(LANG('请输入广告位ID！'));
				return false;
			}
			return true;
		}
		,onDialogOk: function(){
			var self = this;
			var data = self.getValue();
			var listUrl = this.getConfig('listUrl');
			if (self.validate(data)) {
				pubjs.loading.show();
				//获取输入ID对应的数据
				pubjs.mc.send(listUrl, data, self.afterSave.bind(self));
			}
			return false;
		}
		,afterSave: function(err, data){
			pubjs.loading.hide();
			if (err) {
				pubjs.alert(err.message);
				return false;
			}
			if(data && data.total == 0) {
				return pubjs.alert(LANG("导入的广告位失败，请检查相应广告位ID是否正确"))
			}
			
			this.fire('adImportOk', data);
			this.hide();
		}
	});
	exports.spotmport = Spotmport;
	
	var OrderImport = view.widget.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'listUrl': 'deal/list',
				'layout': {
					"module": dialog,
					"title": LANG('导入订单id'),
					"silence": false,
					"buttonConfig": {
						"ok": {
							"value": LANG('确定')
						}
					},
					'size': 'm'
				},
				'view_model': {
					// 媒体广告位ID
					orderIds: ''
				}
			});
			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();
			el.append($([
				'<div class="M-formConGroup">',
				'<label class="group-label">' + LANG('请输入订单ID：') + '</label>',
				'<div class="group-content">',
				'<textarea ms-duplex="orderIds" rows="10" cols="70" placeholder="'+ LANG('请输入订单ID，多个ID请用回车键换行隔开') +'"/>',
				'</div>',
				'</div>'
			].join('')));
			self.buildFromTemplate({
			});
		}
		,getValue: function(){
			var vm = this.vm.$;
			var cs = this.$;
			var orderIds = [];
			var value = vm.orderIds.split('\n');
			value.forEach(function(item){
				var trimItem = util.trim(item);
				if (trimItem) {
					orderIds.push(trimItem);
				}
			});
			var result = {
				'ids': orderIds.join(',')
			};
			return result;
		}
		,setValue: function(data){
			var vm = this.vm.$;
			var cs = this.$;
			vm.orderIds = data.ids.join('\n') || '';
			return this;
		}
		,reset: function(){
			var cs = this.$;
			this.vm.reset();
			return this;
		}
		,validate: function(data){
			if (!data.ids.length) {
				pubjs.alert(LANG('请输入订单ID！'));
				return false;
			}
			return true;
		}
		,onDialogOk: function(){
			var self = this;
			var data = self.getValue();
			var listUrl = this.getConfig('listUrl');
			if (self.validate(data)) {
				pubjs.loading.show();
				//获取输入ID对应的数据
				pubjs.mc.send(listUrl, data, self.afterSave.bind(self));
			}
			return false;
		}
		,afterSave: function(err, data){
			pubjs.loading.hide();
			if (err) {
				pubjs.alert(err.message);
				return false;
			}
			if(data && data.total == 0) {
				return pubjs.alert(LANG("导入的订单失败，请检查相应订单ID是否正确"))
			}
			
			this.fire('adImportOk', data);
			this.hide();
		}
	});
	exports.orderImport = OrderImport;
});